package com.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/api/users")
@Api(value = "用户管理", description = "与系统中的用户相关的操作")
public class UserController {

    @GetMapping("/{userId}")
    @ApiOperation(value = "根据ID获取用户", notes = "返回单个用户的信息", response = User.class)
    public User getUserById(@ApiParam(value = "要检索的用户的ID", required = true) @PathVariable Long userId) {
        // 假设这里是从数据库中获取用户数据
        return new User(userId, "张三");
    }
    @PostMapping("/add")
    public User createUser(@RequestBody User user) {
        // 假设这里是将用户数据保存到数据库中
        return new User( user.getId(), "张三");
    }
    //文件下载
    @GetMapping("/download")
    public ResponseEntity downloadFile() throws IOException {
        String filename = "example.txt";
        String content = "This is an example file content.";
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename)
                .contentType(MediaType.TEXT_PLAIN)
                .body(new ByteArrayResource(content.getBytes()));
    }

    // 假设的 User 类
    public static class User {
        private final Long id;
        private final String name;

        public User(Long id, String name) {
            this.id = id;
            this.name = name;
        }

        // Getters and setters
        public Long getId() {
            return id;
        }

        public String getName() {
            return name;
        }
    }
}
